package cn.xiuxius.embedded.config.validator;
import cn.xiuxius.embedded.config.EasyYamlConfig;
import cn.xiuxius.embedded.config.EasyYamlVersion;

import java.util.Map;

public class ValidatorV1_0_0 implements ConfigValidator {

    @Override
    public void validate(EasyYamlConfig config) {
        if (config == null) {
            throw new IllegalArgumentException("Config cannot be null");
        }

        if (config.getVersion() != EasyYamlVersion.V1_0_0) {
            throw new IllegalArgumentException("Version must be 1.0.0");
        }

        if (config.getEasyStcProject() == null) {
            throw new IllegalArgumentException("easyStcProject cannot be null");
        }
        if (isEmpty(config.getEasyStcProject().getName())) {
            throw new IllegalArgumentException("easyStcProject.name cannot be empty");
        }

        if (config.getKeilProject() == null) {
            throw new IllegalArgumentException("keilProject cannot be null");
        }
        validateKeilProject(config.getKeilProject());

        if (config.getStcgl() == null) {
            throw new IllegalArgumentException("stcgl cannot be null");
        }
        validateStcgl(config.getStcgl());
    }

    private void validateKeilProject(EasyYamlConfig.KeilProject kp) {
        if (isEmpty(kp.getRootPath())) {
            throw new IllegalArgumentException("keilProject.rootPath cannot be empty");
        }
        if (isEmpty(kp.getUvprojPath())) {
            throw new IllegalArgumentException("keilProject.uvprojPath cannot be empty");
        }
        if (kp.getRtos() == null) {
            throw new IllegalArgumentException("keilProject.rtos cannot be null");
        }
        if (kp.getGroups() == null || kp.getGroups().isEmpty()) {
            throw new IllegalArgumentException("keilProject.groups cannot be null or empty");
        }
        for (Map<String, EasyYamlConfig.Group> groupMap : kp.getGroups()) {
            if (groupMap == null || groupMap.isEmpty()) {
                throw new IllegalArgumentException("Each group map inside keilProject.groups cannot be null or empty");
            }
            for (Map.Entry<String, EasyYamlConfig.Group> entry : groupMap.entrySet()) {
                if (entry.getValue() == null) {
                    throw new IllegalArgumentException("Group inside keilProject.groups cannot be null");
                }
                if (isEmpty(entry.getValue().getPath())) {
                    throw new IllegalArgumentException("Group.path inside keilProject.groups cannot be empty");
                }
            }
        }
        if (kp.getIncludes() == null || kp.getIncludes().isEmpty()) {
            throw new IllegalArgumentException("keilProject.includes cannot be null or empty");
        }
        if (kp.getBuild() == null) {
            throw new IllegalArgumentException("keilProject.build cannot be null");
        }
        validateBuild(kp.getBuild());
    }

    private void validateBuild(EasyYamlConfig.Build build) {
        if (build.getMemoryModel() == null) {
            throw new IllegalArgumentException("build.memoryModel cannot be null");
        }
        if (build.getLinkSuffix() == null || build.getLinkSuffix().isEmpty()) {
            throw new IllegalArgumentException("build.linkSuffix cannot be null or empty");
        }
        // genHex is a boolean primitive, no null check needed
    }

    private void validateStcgl(EasyYamlConfig.Stcgl stcgl) {
        if (stcgl.getStcType() == null) {
            throw new IllegalArgumentException("stcgl.stcType cannot be null");
        }
        if (isEmpty(stcgl.getPort().getValue())) {
            throw new IllegalArgumentException("stcgl.port cannot be empty");
        }
    }

    private boolean isEmpty(String s) {
        return s == null || s.trim().isEmpty();
    }
}
